/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.xyplot.xyplot_view.dialog;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.xyplot.xyplot_view.InterfaceXYPlot;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotView;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotViewState;
import br.ufmg.dees.insane.xyplot.xyplot_view.command.LastZoomCommand;
import br.ufmg.dees.insane.xyplot.xyplot_view.command.ZoomAllCommand;

/**
 * A class representing creation dialog on screen.<br> This dialog is provided by some tabs like limits, grid and General settings.<br>
 * @author  Penna Samuel & Pitangueira, Roque
 * @see xyplot_view.InterfaceXYPlot
 * @since  april 2004
 */
public final class StateSettingsDialog extends TabbedDialog {

    /**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InterfaceXYPlot parent;
    private JPanel generalPane = new JPanel();
    private JPanel limitsPane = new JPanel();
    private JPanel gridPane = new JPanel();
    private JPanel colorsPane = new JPanel();
    private JPanel genPane = new JPanel();
    
	private double svdX;
	private double svdY;
	private double svdW;
	private double svdH;
	
	private DecimalField xField;
    private DecimalField yField;
    private DecimalField widthField;
    private DecimalField heightField;
    private DecimalField gridwidthField;
    private DecimalField gridheightField;

	private JTextField gridColorField = new JTextField();
    
	private JTextField backColorField = new JTextField();
    private JTextField pointColorField = new JTextField();
    private JTextField sequenceColorField = new JTextField();
    private JTextField axisColorField = new JTextField();

	private JButton chooseGridColor = new JButton("Choose");
	
	private JButton chooseBackColor = new JButton("Choose");
    private JButton choosePointColor = new JButton("Choose");
    private JButton chooseLineColor = new JButton("Choose");
	   
	private java.awt.geom.Rectangle2D.Double limits = new java.awt.geom.Rectangle2D.Double();
    private String[] precisionStr = {"0", "0.0", "0.00", "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000"};
    private JComboBox precision = new JComboBox(precisionStr);
	//private JColorChooser colorChooser = new JColorChooser();
    private NumberFormat numberFormat;
    private XYPlotViewState state;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see xyplot_view.InterfaceXYPlot
     */
    public StateSettingsDialog(InterfaceXYPlot parent, XYPlotViewState s) {
        this.parent = parent;
        state = s;
		
		svdX = s.getLimits().x;
		svdY = s.getLimits().y;
		svdW = s.getLimits().width;
		svdH = s.getLimits().height;
		
        setUpFormat();
		
        //Sets fields according to model
        limits.setRect(state.getLimits());
        xField = new DecimalField(limits.x, 10, numberFormat);
        yField = new DecimalField(limits.y, 10, numberFormat);
        widthField  = new DecimalField(limits.width, 10, numberFormat);
        heightField = new DecimalField(limits.height, 10, numberFormat);
        precision.setSelectedIndex(state.getPrecision());
		gridheightField  = new DecimalField(state.getGridYInc(), 10, numberFormat);
        gridwidthField  = new DecimalField(state.getGridXInc(), 10, numberFormat);
		
		// Sets up the limits panel
        JPanel limPane = new JPanel();
        limPane.setLayout(new BoxLayout(limPane, BoxLayout.Y_AXIS));
        limPane.add(limitsPane);
        limitsPane.setLayout(new GridLayout(4,2,2,2));
        limitsPane.setMaximumSize(new Dimension(500,120));
        limitsPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Limits bounds"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        limitsPane.add(new JLabel(" X Start:"));
        limitsPane.add(new JLabel(" Y Start:"));
        limitsPane.add(xField);
        limitsPane.add(yField);
        limitsPane.add(new JLabel(" Width:"));
        limitsPane.add(new JLabel(" Height:"));
        limitsPane.add(widthField);
        limitsPane.add(heightField);
        
        //Sets the colors panel
        colorsPane.setLayout(new GridLayout(9,3,2,2));
        colorsPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Colors"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		colorsPane.setMaximumSize(new Dimension(500,300));
        
		colorsPane.add(new JLabel(" Background:"));
        backColorField.setEditable(false);
        backColorField.setBackground(state.bgcolor);
        colorsPane.add(backColorField);
        colorsPane.add(chooseBackColor);
        chooseBackColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                backColorActionPerformed(e);
            }
        } );
        colorsPane.add(new JLabel(" Points:"));
        pointColorField.setEditable(false);
        colorsPane.add(pointColorField);
        colorsPane.add(choosePointColor);
        choosePointColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pointColorActionPerformed(e);
            }
        } );
        colorsPane.add(new JLabel(" Sequence:"));
        sequenceColorField.setEditable(false);
        sequenceColorField.setBackground(state.getSequenceState(0).getLineColor());
        colorsPane.add(sequenceColorField);
        colorsPane.add(chooseLineColor);
        chooseLineColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lineColorActionPerformed(e);
            }
        } );
        colorsPane.add(new JLabel(" Axis:"));
        axisColorField.setEditable(false);
     /*   axisColorField.setBackground(state.getXAxisViewState().getColor());
        colorsPane.add(axisColorField);
        colorsPane.add(chooseMarkColor);
        chooseMarkColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                markColorActionPerformed(e);
            }
        } );
		*/
        //Sets internal general panel
        genPane.setLayout(new GridLayout(1,2,2,2));
        genPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Precision"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		genPane.setMaximumSize(new Dimension(500,55));
        genPane.add(new JLabel(" Precision:"));
        genPane.add(precision);
		
        //Sets general panel
        generalPane.setLayout(new BoxLayout(generalPane, BoxLayout.Y_AXIS));
        generalPane.add(genPane);
        generalPane.add(colorsPane);
		
        // Sets up the grid panel
        JPanel gPane = new JPanel();
        gPane.setLayout(new BoxLayout(gPane, BoxLayout.Y_AXIS));
        gPane.add(gridPane);
        gridPane.setLayout(new GridLayout(4,2,2,2));
        gridPane.setMaximumSize(new Dimension(500,120));
        gridPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Grid settings"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        gridPane.add(new JLabel(" Width Space:"));
        gridPane.add(new JLabel(" Heigt Space:"));
        gridPane.add(gridwidthField);
        gridPane.add(gridheightField);
        gridPane.add(new JLabel(" Color:"));
        gridPane.add(new JLabel());
        gridColorField.setEditable(false);
        gridColorField.setBackground(state.gridColor);
        gridPane.add(gridColorField);
        gridPane.add(chooseGridColor);
        chooseGridColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gridColorActionPerformed(e);
            }
        } );
		
	}	
//*****************************************************************************
    
    public int showDialog() {
        return super.showDialog(parent, "Settings");
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            limits.x = xField.getValue();
            limits.y = yField.getValue();
            limits.height = heightField.getValue();
            limits.width = widthField.getValue();
            			
			state.setGridIncrements(gridwidthField.getValue(), gridheightField.getValue());
            state.gridColor = gridColorField.getBackground();
            
			state.bgcolor = backColorField.getBackground();
            //state.pointColor = pointColorField.getBackground();
           state.getXAxisViewState().setAxisColor(axisColorField.getBackground());		
			state.setLimits(limits);
            state.setPrecision(precision.getSelectedIndex());
			
			//parent.setModel(parent.getModel());
            this.setVisible(false);
			
			boolean changed = false;
			if (svdX != limits.x) changed = true;
			if (svdY != limits.y) changed = true;
			if (svdW != limits.width) changed = true;
			if (svdH != limits.height) changed = true;
			
			if (changed) {
				((XYPlotView)parent.getView()).getDrawingArea().repaint();
				ZoomAllCommand cmd = new ZoomAllCommand(parent);
				cmd.execute();
			}
			else {
				((XYPlotView)parent.getView()).getDrawingArea().repaint();
				LastZoomCommand cmd = new LastZoomCommand(parent);
				cmd.execute();
			}
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
	
    private void gridColorActionPerformed(ActionEvent e) {
        gridColorField.setBackground(JColorChooser.showDialog(this, "Choose Grid Color", gridColorField.getBackground()));
    }
    
//*****************************************************************************
	
    private void backColorActionPerformed(ActionEvent e) {
        backColorField.setBackground(JColorChooser.showDialog(this, "Choose Background Color", backColorField.getBackground()));
    }
    
//*****************************************************************************
	
    private void pointColorActionPerformed(ActionEvent e) {
        pointColorField.setBackground(JColorChooser.showDialog(this, "Choose Points Color", pointColorField.getBackground()));
    }
    
//*****************************************************************************
	
    private void lineColorActionPerformed(ActionEvent e) {
        sequenceColorField.setBackground(JColorChooser.showDialog(this, "Choose Lines Color", sequenceColorField.getBackground()));
    }
	
//*****************************************************************************
	
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    }
	
//*****************************************************************************
}
